package com.eternal.device.model;

import android.app.Application;
import androidx.lifecycle.MutableLiveData;
import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eternal.base.global.ActivityEvent;
import com.eternal.base.global.BluetoothKey;
import com.eternal.base.router.RouterActivityPath;
import com.eternal.device.ChooseActivity;
import com.eternal.device.GuideActivity;
import com.eternal.device.R;
import com.eternal.framework.binding.command.BindingAction;
import com.eternal.framework.binding.command.BindingCommand;
import com.eternal.framework.bus.Messenger;
import com.eternal.framework.component.AppManager;
import com.eternal.framework.component.BaseViewModel;

import static com.eternal.framework.utils.Utils.getString;

/**
 * Created by Administrator
 * On 2020/4/26
 * Description:
 *
 * @author Administrator
 */
public class ChooseModel extends BaseViewModel {
    private byte pageType ;

    public ChooseModel(@NonNull Application application) {
        super(application);
    }

    public BindingCommand<Void> onBack = new BindingCommand<>(new BindingAction() {
        @Override
        public void call() {
            if (pageType == 0) {
                pushToMain();
            } else {
                ARouter.getInstance().build(RouterActivityPath.Device.PAGE_CHOOSE)
                        .withTransition(R.anim.left_in, R.anim.right_out)
                        .navigation(AppManager.getAppManager().currentActivity(), new NavCallback() {
                            @Override
                            public void onArrival(Postcard postcard) {
                                finish();
                            }
                        });
            }
        }
    });

    public BindingCommand<Void> onFirstItemClick = new BindingCommand<Void>(new BindingAction() {
        @Override
        public void call() {
            ARouter.getInstance().build(RouterActivityPath.Device.PAGE_CHOOSE)
                    .withByte(ChooseActivity.PAGE_TYPE, (byte) 0x1)
                    .withTransition(R.anim.right_in, R.anim.left_out)
                    .navigation(AppManager.getAppManager().currentActivity());
        }
    });

    public BindingCommand<Void> onItemAClick = new BindingCommand<Void>(new BindingAction() {
        @Override
        public void call() {
            pushToAddPage(BluetoothKey.DEVICE_TYPE_A);
        }
    });
    public BindingCommand<Void> onItemBClick = new BindingCommand<Void>(new BindingAction() {
        @Override
        public void call() {
            pushToAddPage(BluetoothKey.DEVICE_TYPE_B);
        }
    });
    public BindingCommand<Void> onItemCClick = new BindingCommand<Void>(new BindingAction() {
        @Override
        public void call() {
            pushToAddPage(BluetoothKey.DEVICE_TYPE_C);
        }
    });

    public BindingCommand<Void> onItemEClick = new BindingCommand<Void>(new BindingAction() {
        @Override
        public void call() {
            ARouter.getInstance().build(RouterActivityPath.Device.PAGE_GUIDE)
                    .withTransition(R.anim.right_in, R.anim.left_out)
                    .navigation(AppManager.getAppManager().currentActivity());

        }
    });

    private void pushToAddPage(byte type) {
        ARouter.getInstance().build(RouterActivityPath.Device.PAGE_ADD)
                .withByte(ActivityEvent.DEVICE_TYPE, type)
                .withTransition(R.anim.right_in, R.anim.left_out)
                .navigation(AppManager.getAppManager().currentActivity());
    }

    public BindingCommand<Void> onCancel = new BindingCommand<>(new BindingAction() {
        @Override
        public void call() {
            Messenger.getDefault().send(ChooseModel.this, ChooseActivity.SHOW_DIALOG);
        }
    });

    public void pushToMain(){
        ARouter.getInstance().build(RouterActivityPath.Main.PAGE_MAIN)
                .withTransition(R.anim.left_in, R.anim.right_out)
                .navigation(AppManager.getAppManager().currentActivity(), new NavCallback() {
                    @Override
                    public void onArrival(Postcard postcard) {
                        AppManager.getAppManager().finishActivity(GuideActivity.class);
                        AppManager.getAppManager().finishActivity(ChooseActivity.class);
                    }
                });
    }

    public void init(byte pageType) {
        this.pageType = pageType;
    }
}
